package com.bawei.monthdemo2207a.month01.view

import android.annotation.SuppressLint
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.DateRange
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.bawei.monthdemo2207a.R
import com.bawei.monthdemo2207a.month01.bang.BangPage
import com.bawei.monthdemo2207a.month01.game.GamePage
import com.bawei.monthdemo2207a.month01.home.HomePage
import com.bawei.monthdemo2207a.month01.mine.MinePage
import com.bawei.monthdemo2207a.month01.soft.SoftPage
import com.tencent.mmkv.MMKV

/**
 * 作者：赵亮
 * 时间：2024/12/26 12:11
 * 功能：
 */

data class BottomEntity(val img: Int, val title: String)

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@Composable
fun MainPage(navController: NavController = rememberNavController()) {

    val mmkv = MMKV.defaultMMKV()
    val isQuanXian = mmkv.decodeBool("isQuanXian", false)

    // 是否显示权限对话框
    var isShowQuanXian by remember {
        mutableStateOf(!isQuanXian)
    }

    val list = listOf(
        BottomEntity(R.drawable.nav1, "首页"),
        BottomEntity(R.drawable.nav2, "游戏"),
        BottomEntity(R.drawable.nav3, "软件"),
        BottomEntity(R.drawable.nav4, "榜单"),
        BottomEntity(R.drawable.nav5, "我的"),
    )

    var selectedIndex by remember {
        mutableStateOf(0)
    }

    Box(modifier = Modifier.fillMaxSize()) {
        // 主页面布局
        Scaffold(
            bottomBar = {
                NavigationBar {
                    list.forEachIndexed { index, bottomEntity ->
                        NavigationBarItem(
                            selected = selectedIndex == index,
                            onClick = { selectedIndex = index },
                            label = {
                                Text(text = bottomEntity.title)
                            },
                            icon = {
                                Image(
                                    painter = painterResource(id = bottomEntity.img),
                                    contentDescription = null
                                )
                            })
                    }
                }
            }
        ) {
            when (selectedIndex) {
                0 -> HomePage(navController)
                1 -> GamePage()
                2 -> SoftPage()
                3 -> BangPage()
                4 -> MinePage()
            }
        }
        // 权限弹窗
        if (isShowQuanXian) {
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .background(color = colorResource(id = R.color.welcome_dialog_bg))
            ) {
                Box(
                    modifier = Modifier
                        .padding(15.dp)
                        .align(Alignment.BottomCenter)
                ) {
                    Column(
                        horizontalAlignment = Alignment.CenterHorizontally,
                        modifier = Modifier
                            .background(color = Color.White, shape = RoundedCornerShape(15.dp))
                            .fillMaxWidth()
                    ) {
                        Spacer(modifier = Modifier.height(20.dp))
                        Icon(imageVector = Icons.Default.DateRange, contentDescription = null)
                        Spacer(modifier = Modifier.height(20.dp))

                        Text(
                            text = "允许“软件商店”读取应用列表?",
                            fontSize = 18.sp,
                            fontWeight = FontWeight.Bold
                        )
                        Spacer(modifier = Modifier.height(20.dp))

                        Text(
                            text = "读取应用列表以正常展示待更新应用、去重展示推荐\n" +
                                    "应用", fontSize = 13.sp
                        )
                        Spacer(modifier = Modifier.height(20.dp))

                        Text(text = "允许", color = Color.Blue, modifier = Modifier.clickable {
                            mmkv.encode("isQuanXian", true)
                            isShowQuanXian = false
                        })
                        Spacer(modifier = Modifier.height(20.dp))

                        Text(text = "拒绝", color = Color.Blue, modifier = Modifier.clickable {
                            isShowQuanXian = false
                        })
                        Spacer(modifier = Modifier.height(20.dp))
                    }
                }
            }
        }
    }

}

@Preview(showSystemUi = true)
@Composable
fun MainPagePreview() {
    MainPage()
}

